// pages/pdd/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fx: 1
  },
  tologin() {
    var _ = this;
    wx.showToast({
      title: '请先登录！',
      icon: 'none'
    })
    _.setData({
      login: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    console.log(options);
    _.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
    var phone = wx.getStorageSync('zlphone')
    if (phone) {
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/kanjia/getxzlist?actid=42234&callback=?', //接收url
        data: {
          phone: phone
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          if (theJson.status == 1) {
            _.setData({
              num: theJson.lists.length
            })
            if (theJson.lists.length == 3) {
              _.setData({
                fx: 3
              })
            } else {
              _.setData({
                fx: 2
              })
            }
          } else {
            _.setData({
              fx: 2,
              num: 0
            })
          }
        }
      })
    } else {
      _.setData({
        fx: 1
      })
    }

  },

  getPhoneNumber(e) {
    var _ = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      _.setData({
        mobile: res.result,
      })
      console.log(res.result)
      wx.setStorageSync('zlphone', res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/kanjia/register?actid=42234&callback=?', //接收url
        data: {
          phone: _.data.mobile,
          fenxiao: _.data.area,
          geneal: _.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)
          wx.request({ //发送请求
            url: 'https://zg99.offcn.com/index/kanjia/writeyq?actid=42234&callback=?', //接收url
            data: {
              phone: _.data.mobile,
              spid: 2,
            }, //接收参数
            success(res) {
              var jsonStr = res.data;
              jsonStr = jsonStr.replace("(", "");
              jsonStr = jsonStr.replace(")", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
              }
              console.log(theJson)
              _.setData({
                login: !1,
                fx: 2
              })
            }
          })
        }
      });
      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 17,
          mobile: res.result,
          areaCode: _.data.area,
          agent: _.data.geneal,
          pageId: _.data.page
        },
        success: res => {
          console.log(res);
        }
      })
    })

  },
  tothr() {
    wx.navigateTo({
      url: '../res/res',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _ = this;
    return {
      title: '快来帮我助力领取免费课程！',
      path: '/pages/pdd/zhuli/zhuli?yqphone=' + _.data.mobile + '&area=' + _.data.area + '&agent=' + _.data.geneal + '&page=' + _.data.page
    }
  }
})